package com.douqu.game.core.entity.db;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.entity.ext.data.BaseData;
import com.douqu.game.core.protobuf.SGCommonProto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2017-10-24 16:21
 */
public class InstanceListDB extends BaseData {
    /**
     * 已经完成的地图列表
     */
    private List<InstanceDB> instanceList;

    private Map<Integer, InstanceDB> instanceMap;





    public InstanceListDB()
    {
        instanceList = new CopyOnWriteArrayList<>();
        instanceMap =  new ConcurrentHashMap();
    }

    public void addInstanceDB(InstanceDB db){
        if(db != null){
            instanceList.add(db);
            instanceMap.put(db.id, db);
        }

    }


    public int getTotalStars(){
        int sum = 0;
        for(InstanceDB db : instanceList){
            sum += db.getTotalStarts();
        }
        return sum;
    }

    /**
     * 是否有红点消息
     * @return
     */
    public boolean hasRedPointRemind(){
        for(InstanceDB instanceDB : instanceList){
            if(instanceDB.hasRedPointRemind()){
                return true;
            }
        }
        return false;
    }


    @Override
    public void init()
    {

    }

    @Override
    public void checkInit() {

    }

    @Override
    public void reset()
    {

    }

    @Override
    public void checkReset() {

    }

    @Override
    public void writeTo(ByteBuffer buffer)
    {
        buffer.writeInt(instanceList.size());
        for(InstanceDB db : instanceList){
            db.writeTo(buffer);
        }
        buffer.writeBoolean(false);
    }

    @Override
    public void loadFrom(ByteBuffer buffer)
    {
        int size = buffer.readInt();
        for(int i =0; i < size; i++){
            InstanceDB db = new InstanceDB();
            db.loadFrom(buffer);
            addInstanceMap(db);
        }
        buffer.readBoolean();
    }

    public InstanceDB getInstanceMap(int mapId)
    {
        return instanceMap.get(mapId);
    }

    public void addInstanceMap(InstanceDB map){
        if(map != null && !instanceMap.containsKey(map.id)){
            instanceList.add(map);
            instanceMap.put(map.id, map);

        }
    }




    public List<SGCommonProto.InstanceMap> getPropInstanceMap(){
        List<SGCommonProto.InstanceMap> list = new ArrayList<>();
        for(InstanceDB instanceDB : instanceList){
            list.add(SGCommonProto.InstanceMap.newBuilder().setChapterId(instanceDB.id).
                    addAllReciverRewards(instanceDB.getHasReward()).addAllLevels(instanceDB.getPropLevel()).build());
        }
        return list;
    }




    public List<InstanceDB> getInstanceList() {
        return instanceList;
    }


    public Map<Integer, InstanceDB> getInstanceMap() {
        return instanceMap;
    }

}